const app = getApp ();
import {getDate, convertDate, getTime} from '../../utils/date';
import {
  getPlanInfoApi,
  deleteTeamApi,
  returnTeamApi,
  editAgrencyApi,
} from '../../api/app';
Page ({
  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    infoData: null,
    isCaptain: false,
    daySelect: ['工作', '学习', '娱乐', '生活', '其他'], // 类型
    currentDate: getDate (new Date (), '-'),
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData ({
      id: options.id,
      openid: wx.getStorageSync ('openId'),
    });
  },

  onShow: function () {
    this.getInfo ();
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '快来加入协同小程序吧~',
    };
  },
  toEdit () {
    wx.navigateTo ({
      url: `/subPages/clockin/create-book/index?data=${JSON.stringify (this.data.infoData)}`,
    });
  },
  chooseAddress () {
    wx.chooseAddress ({
      success: result => {
        console.log (result);
      },
      fail: err => {
        console.log (err);
      },
    });
  },
  onChange (e) {
    this.editState (e);
  },
  // 修改状态
  async editState (e) {
    try {
      const res = await editAgrencyApi ({
        status: e.detail ? '1' : 0,
        id: this.data.infoData.id,
      });
      if (res.code == 200) {
        this.getInfo ();
      }
      // wx.showToast ({
      //   title: res.msg,
      //   icon: 'none',
      // });
    } catch (error) {
      console.log (error);
    }
  },
  // 获取
  async getInfo () {
    const openid = wx.getStorageSync ('openId');
    try {
      // read或空表示阅读打卡，sport表示运动打卡
      const res = await getPlanInfoApi ({
        id: this.data.id,
      });
      if (res.code == 200 && res.result) {
        this.setData ({
          infoData: res.result,
          isCaptain: res.result.uid == openid,
        });
      }
    } catch (error) {
      console.log (error);
    }
  },
  // 复制
  getCode () {
    wx.setClipboardData ({
      data: this.data.infoData.code,
      success (res) {
        wx.showToast ({
          title: '复制成功',
          icon: 'none',
        });
      },
    });
  },
  toJoin () {
    wx.navigateTo ({
      url: '/subPages/joinTeam/index',
      success: result => {},
      fail: () => {},
      complete: () => {},
    });
  },
  // 选择日期
  selectStartDate (e) {
    const startDate = new Date (e.detail.value).getTime ();
    this.setData ({
      startDate: getDate (new Date (startDate), '-'),
    });
  },
  // 选择运动天数
  selectType (e) {
    const motionDayValue = Number (e.detail.value);
    this.setData ({
      motionDayValue,
    });
  },
  // 监听输入框改变
  bindinput (e) {
    const {currentTarget, detail} = e;
    let index = currentTarget.dataset.index;
    let value = detail.value;
    const list = this.data.planList;
    list[index].teamPlan = value;
    this.setData ({
      planList: list,
    });
    console.log (e);
  },
  // 提交
  async submit () {
    const uid = wx.getStorageSync ('openId') || '';
    console.log (this.data.planList);
    try {
      const res = await createTeamApi ({
        name: this.data.teamName,
        memo: this.data.desc,
        uid,
        planDOS: this.data.planList,
      });
      if (res.code == 200) {
        wx.showToast ({
          title: '创建成功',
          icon: 'none',
        });
        setTimeout (() => {
          wx.navigateBack ({
            delta: 1,
          });
        }, 1500);
      } else {
        wx.showToast ({
          title: '创建失败',
          icon: 'none',
        });
      }
    } catch (error) {
      console.log (error);
    }
  },

  handleReturn () {
    wx.showModal ({
      title: '提示',
      content: '你确定要退出当前协助？',
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '确定',
      confirmColor: '#3CC51F',
      success: result => {
        if (result.confirm) {
          this.backTeam (wx.getStorageSync ('openId'));
        }
      },
      fail: () => {},
      complete: () => {},
    });
  },
  handleDelete () {
    wx.showModal ({
      title: '提示',
      content: '你确定要删除该协助？',
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '确定',
      confirmColor: '#3CC51F',
      success: result => {
        if (result.confirm) {
          this.deleteTeam ();
        }
      },
      fail: () => {},
      complete: () => {},
    });
  },
  handleRemove (e) {
    wx.showModal ({
      title: '提示',
      content: '你确定要移除该成员？',
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '确定',
      confirmColor: '#3CC51F',
      success: result => {
        if (result.confirm) {
          this.backTeam ();
        }
      },
      fail: () => {},
      complete: () => {},
    });
  },
  // 退出团队
  async backTeam () {
    try {
      const res = await returnTeamApi ({
        openID: wx.getStorageSync ('openId'),
        id: this.data.infoData.id,
      });
      if (res.code == 200) {
        wx.showToast ({
          title: '退出成功！',
          icon: 'none',
        });
        setTimeout (() => {
          wx.navigateBack ({
            delta: 1,
          });
        }, 1500);
      }
    } catch (error) {
      console.log (error);
    }
  },
  // 删除团队
  async deleteTeam () {
    try {
      const res = await deleteTeamApi ({
        openID: wx.getStorageSync ('openId'),
        id: this.data.infoData.id,
      });
      if (res.code == 200) {
        wx.showToast ({
          title: '删除成功！',
          icon: 'none',
        });
        setTimeout (() => {
          wx.navigateBack ({
            delta: 1,
          });
        }, 1500);
      }
    } catch (error) {
      console.log (error);
    }
  },
});
